using RAG.Application.Services.Interfaces;
using RAG.Domain.Entities.App;
using RAG.Domain.Repositories;

namespace RAG.Application.Services.Implementations;

/// <summary>
/// 会话服务实现
/// </summary>
public class SessionService : ISessionService
{
    private readonly IRepository<Sessions> _sessionRepository;

    public SessionService(IRepository<Sessions> sessionRepository)
    {
        _sessionRepository = sessionRepository;
    }

    /// <summary>
    /// 获取或创建会话
    /// </summary>
    public async Task<Guid> GetOrCreateSessionAsync(Guid? sessionId, Guid userId, string sessionName = "新会话")
    {
        if (sessionId.HasValue)
        {
            // 检查会话是否存在
            var existingSession = await _sessionRepository.GetByIdAsync(sessionId.Value);
            if (existingSession != null)
            {
                return sessionId.Value;
            }
        }

        // 创建新会话
        var newSession = new Sessions
        {
            Id = Guid.NewGuid(),
            SessionName = sessionName,
            Status = "active",
            SessionType = "chat",
            Description = $"用户 {userId} 的会话",
            LastActiveTime = "刚刚",
            UserId = userId,
            CreatedAt = DateTime.UtcNow,
            UpdatedAt = DateTime.UtcNow,
            CreatedBy = userId,
            UpdatedBy = userId
        };

        await _sessionRepository.CreatedAsync(newSession);
        return newSession.Id;
    }
}

